package com.cmdi.opensite.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cmdi.opensite.entity.Activity;
import com.cmdi.opensite.mapper.ActivityMapper;
import com.cmdi.opensite.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 活动服务实现类
 */
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements ActivityService {
    
    @Autowired
    private ActivityMapper activityMapper;
    
    @Override
    public IPage<Activity> selectActivityPage(IPage<Activity> page, Long categoryId, Integer status, String keyword) {
        return activityMapper.selectActivityList(page, categoryId, status, keyword);
    }
    
    @Override
    public Activity getActivityById(Long id) {
        return activityMapper.selectActivityById(id);
    }
    
    @Override
    public boolean saveActivity(Activity activity) {
        // 如果是新增活动，设置初始参与人数为0
        if (activity.getId() == null) {
            activity.setCurrentParticipants(0);
            activity.setStatus(0); // 默认为草稿状态
        }
        return saveOrUpdate(activity);
    }
    
    @Override
    public boolean updateActivityStatus(Long id, Integer status) {
        Activity activity = new Activity();
        activity.setId(id);
        activity.setStatus(status);
        return updateById(activity);
    }
    
    @Override
    public boolean deleteActivity(Long id) {
        // 在删除前应该检查是否有报名记录
        return removeById(id);
    }
    
    @Override
    public List<Activity> getUpcomingActivities(int limit) {
        return activityMapper.selectUpcomingActivities(new Date(), limit);
    }
    
    @Override
    public List<Activity> getHotActivities(int limit) {
        return activityMapper.selectHotActivities(limit);
    }
    
    @Override
    public IPage<Activity> getUserRegisteredActivities(IPage<Activity> page, Long userId) {
        return activityMapper.selectUserRegisteredActivities(page, userId);
    }
    
    @Override
    public IPage<Activity> getUserCreatedActivities(IPage<Activity> page, Long organizerId) {
        return activityMapper.selectUserCreatedActivities(page, organizerId);
    }
    
    @Override
    public boolean updateParticipantCount(Long activityId, Integer increment) {
        return activityMapper.updateParticipantCount(activityId, increment) > 0;
    }
}